﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace xiaolanshu
{
    public partial class 注册 : Form
    {
        public 注册()
        {
            InitializeComponent();
        }

        private void 注册_Load(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            SqlConnection conn = SQLTools.conn;
            conn.Open();
            string sql1 = @"select 用户名 from 用户";
            SqlCommand cmd = new SqlCommand(sql1, conn);
            SqlDataReader rd = cmd.ExecuteReader();
            if (textBox2.Text.Length > 10)
            {
                label1.Text = "(超过十个字，请重新输入！)";
                textBox2.Text = "";
                textBox2.Focus();
            }

            else if (rd.Read())
            {
                while ((rd[0].ToString()).CompareTo(textBox2.Text) == 0)
                {
                    label1.Text = "(此用户名已被使用，请重新输入！)";
                    textBox2.Text = "";
                    textBox2.Focus();
                    break;

                }
            }
            else { label1.Text = "(此用户名可正常使用！)"; }
            rd.Close();
            conn.Close();
        }

        private void textBox5_Leave(object sender, EventArgs e)
        {
            if (textBox4.Text.CompareTo(textBox5.Text) != 0)
            {
                textBox5.Text = "";
                textBox5.Focus();
                label7.Text = "密码和上次输入的不同！";
            }
        }

        private void textBox4_Leave(object sender, EventArgs e)
        {
            SqlConnection conn = SQLTools.conn;
            conn.Open();
            string sql1 = @"select 密码 from 用户";
            SqlCommand cmd = new SqlCommand(sql1, conn);
            SqlDataReader rd = cmd.ExecuteReader();
            if (textBox4.Text.Length > 10)
            {
                label9.Text = "(超过十个字符，请重新输入！)";
                textBox4.Text = "";
                textBox4.Focus();
            }
            else if (rd.Read())
            {
                while ((rd[0].ToString()).CompareTo(textBox4.Text) == 0)
                {
                    label9.Text = "(此密码已被使用，请重新输入！)";
                    textBox4.Text = "";
                    textBox4.Focus();
                    break;

                }
            }
            else { label9.Text = "(此密码可正常使用！)"; }
            rd.Close();
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (textBox4.Text.CompareTo(textBox5.Text) == 0)
            {
                SqlConnection conn = SQLTools.conn;

                string sql1 = @"insert into 用户 values(@a,@b,@c,@d,@e)";
                SqlCommand cmd = new SqlCommand(sql1, conn);
                cmd.Parameters.Add("@a", SqlDbType.VarChar, 20).Value = textBox2.Text;
                cmd.Parameters.Add("@b", SqlDbType.Char, 2);
                cmd.Parameters.Add("@c", SqlDbType.VarChar, 10).Value = color.Value.ToString();
                cmd.Parameters.Add("@d", SqlDbType.Char, 11).Value = textBox3.Text;
                cmd.Parameters.Add("@e", SqlDbType.VarChar, 10).Value = textBox4.Text;
                if (radioButton1.Checked)
                    cmd.Parameters["@b"].Value = "男";
                else cmd.Parameters["@b"].Value = "女";
                /*
                if (radioButton3.Checked)
                    cmd.Parameters["@c"].Value = "干皮";
                else if (radioButton4.Checked)
                    cmd.Parameters["@c"].Value = "油皮";
                else
                    cmd.Parameters["@c"].Value = "混干皮";
                */

                Console.WriteLine("hello!!");

                try
                {
                    Console.WriteLine("wttwtt");
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("注册成功！");
                }
                catch (SqlException ex)
                { MessageBox.Show(ex.Message, "失败"); }
                finally { conn.Close(); }
                this.Close();
            }
        }

        private void color_ValueChanged(object sender, EventArgs e)
        {
            
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}
